import { defineStore } from "pinia";
import { ref } from "vue";

// 定义一个pinia store
export const useStore = defineStore(
  "store",
  () => {
    const state = ref({
      name: "",
      age: "",
      token: "",
    });

    // 定义一个方法来修改state
    const setState = (newState) => {
      state.value = newState;
    };

    //   定义一个方法清空state
    const clearState = () => {
      state.value = {};
    };

    //   向外导出
    return {
      state,
      setState,
      clearState,
    };
  },
  //   持久化存储信息
  {
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key);
        },
        setItem(key, value) {
          return uni.setStorageSync(key, value);
        },
      },
    },
  }
);
